<?php 
declare (strict_types = 1);
namespace app\api\controller;
use app\common\controller\Api;
use think\Request;
class BusinessUser extends Api
{
    protected $with = ['user_index'];
    //商户端-我的用户-列表
    public function index()
    {
        $this->businessCheckLogin();
        $post = param();
        $limit = input('limit') ?: 20;
        $arr = [];
        $arr['zs'] = $this->model->where('business_id','=',$this->businessinfo['id'])->count('id');
        $arr['now_zs'] = $this->model->where([
            ['business_id','=',$this->businessinfo['id']],
            ['create_time','>=',date('Y-m-d')]
        ])->count('id');
        if(isset($post['where'])){
            $data = $this->model->where('business_id','=',$this->businessinfo['id'])->select();
            if(!empty($data) && !empty($data->toArray()))
            {
                $data = $data->toArray();
                $ids = [];
                foreach ($data as $k=>$v)
                {
                    $ids[] = $v['user_index_id'];
                }
                $ids = implode(',',$ids);
                $UserIndex_model = app('app\api\model\UserIndex');
                $data = $UserIndex_model->where('id','in',$ids)->where('nickname|tel','=',$post['where'])->select();
                if(!empty($data) && !empty($data->toArray()))
                {
                    $data = $data->toArray();
                    $ids = [];
                    foreach ($data as $v){
                        $ids[] = $v['id'];
                    }
                    $ids = implode(',',$ids);
                    $data = $this->model->with($this->with)->where([
                        ['business_id','=',$this->businessinfo['id']],
                        ['user_index_id','in',$ids]
                    ])->order('id desc')->paginate($limit);
                    $arr['data'] = $data;
                    return datasuccess($arr);
                }else{
                    return datasuccess();
                }
            }else{
                return datasuccess();
            }
        }
        $data = $this->model->where([['business_id','=',$this->businessinfo['id']]])->with($this->with)->paginate($limit);
        $arr['data'] = $data;
        return datasuccess($arr);
    }
}